import util from "@/libs/util";
import Config from "@/config/app.js";
/**
 * 重置对象（会修改原始对象）
 * @param object
 * @param defaultVal
 */
export function resetObject(object, defaultVal = {}) {
  for (let k in object) {
    if (defaultVal.hasOwnProperty(k)) {
      object[k] = defaultVal[k];
    } else {
      if (Array.isArray(object[k])) object[k] = [];
      if ("string" == typeof object[k]) object[k] = "";
      if ("number" == typeof object[k]) object[k] = null;
      if ("boolean" == typeof object[k]) object[k] = false;
    }
  }
}

/**
 * 对象赋值（会修改原始对象）
 * @param object
 * @param valObject
 */
export function fillerLeft(object, valObject = {}) {
  for (let k in object) {
    if (valObject.hasOwnProperty(k)) {
      object[k] = valObject[k];
    }
  }
}

export function requestHeaders() {
  return {
    Accept: "*/*",
    businessSource: 3,
    terminalPlatform: 4,
    requestId: new Date().getTime(),
    Authorization: util.cookies.get("token"),
    platform: Config.platformUUID
  };
}

export function obj2StrParams(obj, firstStr = "?") {
  let params = firstStr;
  for (let p in obj) {
    let isFirst = params === "?";
    let qz = isFirst ? "" : params === "&" ? "" : "&";
    params += qz + p + "=" + obj[p];
  }
  return params;
}

//保留每页数据查询
export function retainPaginationParams(name, params = {}) {
  if (params === {}) return;
  sessionStorage.setItem(name + "Params", JSON.stringify(params));
}
//获取每页数据查询
export function obtainPaginationParams(name, searchParams = {}) {
  if (!name) return;
  let sessionParams = sessionStorage.getItem(name + "Params")
    ? JSON.parse(sessionStorage.getItem(name + "Params"))
    : null;
  sessionStorage.removeItem(name + "Params");
  let params = sessionParams ? sessionParams : searchParams;
  return params;
}
